<template>
   <!-- <el-button @click="drawer = true" type="primary" style="margin-left: 16px;">
        点我打开
    </el-button>-->
    <div>
        <el-drawer
                :before-close="handleClose"
                :append-to-body="true"
                :visible.sync="dialogVip"
                :direction="direction">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm demo-form-inline" :inline="true" size="medium">
                <el-form-item label="姓名" prop="name" style="margin-right: -20px">
                    <el-input v-model="ruleForm.name" style="width:120px;" :placeholder="dataEmp.name"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone"  style="margin-left: -10px">
                    <el-input type="phone" maxlength="11"  v-model.number="ruleForm.phone" autocomplete="off" style="width:140px" :placeholder="dataEmp.ephone"></el-input>
                </el-form-item>
                <el-form-item label="身份证号" prop="idCard"  style="margin-left: -10px">
                    <el-input v-model="ruleForm.idCard" style="width:auto;" maxlength="18" :placeholder="dataEmp.eidCard"></el-input>
                </el-form-item>
                <el-form-item label="常驻地址" prop="address" style="margin-left: -20px">
                    <el-input v-model="ruleForm.address" style="width:260px;" maxlength="18" :placeholder="dataEmp.address"></el-input>
                </el-form-item>
                <el-form-item label="薪资" prop="sal"  style="margin-left: -20px;">
                    <el-input type="phone" maxlength="18"  v-model.number="ruleForm.sal" autocomplete="off" style="width:100px" :placeholder="dataEmp.sal"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="Submit" :loading="loading" style="margin-left: 660px;margin-right: 10px">{{ loading ? '提交中 ...' : '提交修改' }}</el-button>
                    <el-button @click="cancelForm">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </div>

</template>

<script>
    export default {
        name: "EditEmp",
        inject:['reload'],
        props:{
          dataEmp:{}
        },
        data() {
            return {
                direction: 'btt',
                loading:false,
                ruleForm: {
                    name: '',
                    phone: '',
                    idCard: '',
                    address: '',
                    sal: '',
                },
                rules: {
                    name: [
                        { required: true, message: '请输入乘客姓名', trigger: 'blur' },
                        { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入电话',trigger: 'blur'},
                        { type: 'number', message: '电话必须为数字值', trigger: 'blur'},
                    ],
                    idCard: [
                        { required: true, message: '请输入身份证号码',trigger: 'blur'},
                    ],
                    address: [
                        { required: true, message: '请输入常驻地址',trigger: 'blur'},
                    ],
                    sal: [
                        { required: true, message: '请输入员工薪资',trigger: 'blur'},
                        { type: 'number', message: '薪资必须为数字值', trigger: 'blur'},
                    ],

                },
                timer: null,
            }
        },
        methods:{
            handleClose(done) {
                done();
                //点击抽屉以外，直接回到列表页面
                this.loading = false;
                this.$store.commit('setDialog',false);
                clearTimeout(this.timer);
            },
            cancelForm() {
                //点击取消按钮
                this.loading = false;
                this.$store.commit('setDialog',false);
                clearTimeout(this.timer);
            },
            Submit() {
                //点击提交按钮
                if (this.loading) {
                    return;
                }
                //验证表单
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        console.log('表单验证通过！');
                        this.$confirm('确定要提交修改吗？')
                            .then(_ => {
                                //提交表单
                                //alert("提交表单");
                                this.loading = true;
                                this.timer = setTimeout(() => {
                                    let emp = new URLSearchParams();
                                    //alert("员工编号"+JSON.stringify(this.dataEmp));
                                    emp.append('eid', this.dataEmp.eid);
                                    emp.append('ename', this.ruleForm.name);
                                    emp.append('ephone', this.ruleForm.phone);
                                    emp.append('eidCard',this.ruleForm.idCard);
                                    emp.append('eaddress',this.ruleForm.address);
                                    emp.append('sal',this.ruleForm.sal);

                                    // 动画关闭需要一定的时间
                                    setTimeout(() => {
                                        this.axios.post("/emp/update",emp)
                                            .then(res=>{
                                                if (res.data==""){
                                                    this.$notify({
                                                        message: "权限不足,请联系高级管理员",
                                                        type: 'error'
                                                    });
                                                }else{
                                                    this.$notify({
                                                        message: res.data,
                                                        type: 'success'
                                                    });
                                                }
                                                //关闭面板
                                                this.$store.commit('setDialog',false);
                                                //重新渲染（CSDN上的快速刷新页面）
                                                this.reload();
                                            });
                                        this.loading = false;
                                    }, 700);
                                }, 700)
                            })
                            .catch(_ => {
                                //不提交表单，回到列表页面
                                this.loading = false;
                                this.$store.commit('setDialog',false);
                                clearTimeout(this.timer);
                            });
                    } else {
                        //表单验证失败
                        this.$message.error('请正确填写员工修改信息');
                        return false;
                    }
                });

            },
        },
        computed:{
            dialogVip: {
                get() {
                    return this.$store.getters.dialogVip;
                },
                set() {
                    //解决警告
                }
            }
        },
    }
</script>

<style scoped>

</style>
